const int dirR = 12;
const int dirL = 13;  // not used in this example
const int speedR = 3;
const int speedL = 11; // not used in this example

const byte FORWARD = 0;
const byte REVERSE = 1;
const byte LEFT = 2;
const byte RIGHT = 3;

boolean moveA = true;
boolean moveB = true;

void setup()
{  
  pinMode (dirR, OUTPUT);
  pinMode (dirL, OUTPUT);
  pinMode (speedR, OUTPUT);
  pinMode (speedL, OUTPUT);

  stopMotors();
}

void stopMoving()
{
  digitalWrite(speedL, LOW);  
  digitalWrite(speedL, LOW);  
}

void stopMotors()
{
  stopMoving();
}

void moveForward()
{
  moveForward(255);
}

void moveForward(int vel)
{
  moveForward(vel, -1);
}

void moveForward(int vel, int time)
{
  move(FORWARD, vel, time);
}

void moveReverse()
{
  moveReverse(255);
}

void moveReverse(int vel)
{
  moveReverse(vel, -1);
}

void moveReverse(int vel, int time)
{
  move(REVERSE, vel, time);
}


void move(byte dir)
{
  move(dir, 255);
}

void move(byte dir, int vel)
{
  move(dir, vel, -1);
}

void move(byte dir, int vel, int time)
{
  switch ( dir ) 
  {
    case FORWARD:
      digitalWrite (dirR, HIGH); 
      digitalWrite (dirL, HIGH);         
      break;
    case REVERSE:
      digitalWrite (dirR, LOW); 
      digitalWrite (dirL, LOW);         
      break;
    case LEFT:
      digitalWrite (dirR, HIGH); 
      digitalWrite (dirL, LOW);         
      break;
    case RIGHT:
      digitalWrite (dirR, LOW); 
      digitalWrite (dirL, HIGH);         
      break;
    default:
      break;
  }    

  analogWrite (speedR, vel);
  analogWrite (speedL, vel);
  if(time>0)
  {
    delay (time);
    digitalWrite(speedL, LOW);  
    digitalWrite(speedR, LOW); 
  }
}


void loop()
{
  
  
  //Forward
  moveForward(170, 1000);

  delay(2000);
  
  //Rewind
  moveReverse(1000, 1000);
  
  delay(2000);
  
  //Left
  digitalWrite (dirR, LOW); 
  digitalWrite (dirL, HIGH);   
  analogWrite (speedR, 255);
  analogWrite (speedL, 255);   
  delay (1000);
  analogWrite (speedR, 0);
  analogWrite (speedL, 0); 

  delay(2000);
  
  //Right
  digitalWrite (dirR, HIGH); 
  digitalWrite (dirL, LOW);   
  analogWrite (speedR, 255);
  analogWrite (speedL, 255);   
  delay (1000);
  analogWrite (speedR, 0);
  analogWrite (speedL, 0); 
  
  
  //Forward increasing and decreasing
  
 
  
  
  delay(5000);

/* 
  if(moveA)
  {
    // move the motor A to one direction increasing speed
    // and stopping decreasing speed
    digitalWrite (dirA, HIGH);    
    for (int j = 0; j < 255; j += 10) {
       analogWrite (speedA, j);
       delay (500);
    }
    delay(1000); // keep the motor rolling for one second  
    for (int j = 255; j >= 0; j -= 10) {
       analogWrite (speedA, j);
       delay (500);
    }
  
    // stop the motors for 0.5 seconds
    stopMotors();
    delay(500); // keep the motor rolling for one second
  
    // move the motor A to the other direction increasing speed
    // and stopping decreasing speed
    digitalWrite (dirA, LOW);
    for (int j = 0; j < 255; j += 10) {
       analogWrite (speedA, j);
       delay (100);
    }
    delay(1000); // keep the motor rolling for one second
    for (int j = 255; j >= 0; j -= 10) {
       analogWrite (speedA, j);
       delay (100);
    }
  }
  
  
  if(moveB)
  {
    // move the motor B to one direction increasing speed
    // and stopping decreasing speed
    digitalWrite (dirB, HIGH);
    for (int j = 0; j < 255; j += 10) {
       analogWrite (speedB, j);
       delay (100);
    }
    delay(1000); // keep the motor rolling for one second  
    for (int j = 255; j >= 0; j -= 10) {
       analogWrite (speedB, j);
       delay (100);
    }
  
    // stop the motors for 0.5 seconds
    stopMotors();
    delay(500); // keep the motor rolling for one second
  
    // move the motor B to the other direction increasing speed
    // and stopping decreasing speed
    digitalWrite (dirB, LOW);
    for (int j = 0; j < 255; j += 10) {
       analogWrite (speedB, j);
       delay (100);
    }
    delay(1000); // keep the motor rolling for one second
    for (int j = 255; j >= 0; j -= 10) {
       analogWrite (speedB, j);
       delay (100);
    }
  }
*/

  stopMotors();
  delay(3000); // keep the motor stopped for three seconds

}
